﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2017-2018 Jean-Marc VIGLINO,
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Select control</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="Control to select features by attributes	." />
  <meta name="keywords" content="ol3, control, select, vector, feature, attribute" />

  <!-- FontAwesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <link rel="stylesheet" href="../style.css"/>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>
  <a href="../../index.html">
    <h1>ol-ext: FeatureList control</h1>
  </a>

  <div class="info">
    <p>
      <i>ol/control/FeatureList</i> is a control for displaying features in a list.
    </p>
    <p>
      The list is optimzed to display large datasets in the table.
    </p>
    <ul>
      <li>
        You have to define a <i>ol/source/Vector</i> or a list of feature to display in the table.
      </li>
      <li>
        The control use optimization to display large dataset, only visible rows (page) are displayed in the table.
        <br/>
        Set the <i>pageLength</i> property to enlarge visible rows size (default 200 rows).
      </li>
      <li>
        The list can be ordered and sized by the user.
      </li>
      <li>
        Use the <i>select</i> (or <i>dblclick</i>) event to acknowledge of a clik on a row (cell).
      </li>
    </ul>
  </div>

  <!-- DIV pour la carte -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options" style="max-width:20em;">
    <h2>Options:</h2>
    <i>Click on the map to select a feature / use the control to look for features by attributes.</i>
    <br/>
    <button onclick="javascript:listCtrl.scrollTo('select')">Return to sélection</button>
    <ul style="list-style:initial; padding: 0 1em;"></ul>
  </div>
  
  <script type="text/javascript">
  // Layers
  var layers = [
    new ol.layer.Tile({
			title:'OSM',
			source: new ol.source.OSM()
		})
  ]

  // The map
  var map = new ol.Map({
    target: 'map',
    view: new ol.View({
      zoom: 5,
      center: [166326, 5992663]
    }),
    layers: layers
  });

  // GeoJSON layer
  var vectorSource = new ol.source.Vector({
    url: '../data/departements.geojsonx',
    // url: '../data/habitats_doubs.geojsonx',
    format: new ol.format.GeoJSONX(),
    /* large dataset * /
    url: 'https://raw.githubusercontent.com/IGNF-Ma-carte/mcstatistic/main/assets/data/example/USGS-Earthquake-2015.json',
    format: new ol.format.GeoJSON(),
    /**/
    attributions: [ "&copy; <a href='https://www.insee.fr'>INSEE</a>", "&copy; <a href='https://www.data.gouv.fr/fr/datasets/geofla-r/'>IGN</a>" ],
  });

  map.addLayer(new ol.layer.VectorImage({
    name: 'Departements',
    source: vectorSource
  }));

  // Select  interaction
  var selecti = new ol.interaction.Select({
    hitTolerance: 5,
    condition: ol.events.condition.singleClick
  });
  map.addInteraction(selecti);
  // Select feature when click on the reference index
  selecti.on('select', function(e) {
    var f = e.selected[0];
    if (f) {
      showInfo(f)
      listCtrl.select(f)
    }
  });

  function showInfo(f) {
    var prop = f.getProperties();
    var ul = $('.options ul').html('');
    for (var p in prop) if (p!=='geometry') {
      $('<li>').text(p+': '+prop[p]).appendTo(ul);
    }
  }

  // Select control
  var listCtrl = new ol.control.FeatureList({
    title: 'Communes',
    // className: 'ol-bottom',
    collapsed: false,
    features: vectorSource,
    // number: 20,
    // target: document.body
  });
  listCtrl.enableSort('nom', 'region', 'mag')
  map.addControl (listCtrl);
  listCtrl.on('select', function(e) {
    selecti.getFeatures().clear();
    selecti.getFeatures().push(e.feature);
    showInfo(e.feature)
  });
  listCtrl.on('dblclick', function(e) {
    map.getView().fit(e.feature.getGeometry().getExtent())
    map.getView().setZoom(map.getView().getZoom() - 1)
  })

  listCtrl.on(['resize', 'collapse', 'sort'], function(e) {
    console.log(e)
  })


  </script>
  
</body>
</html>